package com.multipleThread.myRestaurant;

import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * @author 罗俊华
 * @date 2021/4/29 - 1:49 下午
 */
public class WaitingPerson {

    private final RestaurantPlus restaurantPlus;

    public WaitingPerson(RestaurantPlus restaurantPlus) {
        this.restaurantPlus = restaurantPlus;
    }

    /**
     * 客户下单的时候调用此方法
     *
     * @param customer
     * @param food
     */
    public void placeAnOrder(Customer customer, Food food) {
        OrderTicket orderTicket = getOrderTicketByTableId(customer.getTable());
//      将菜加入该桌的订单
        orderTicket.getFoodStatistic().put(food.getFoodId(), false);
//      将用户点的食物添加到待烹饪的订单中
        this.restaurantPlus.getNewOrderedMenuQueue().add(new Menu(orderTicket.getTable(), food));
//      提交用户点的菜单之后，自己就没事干了
        this.restaurantPlus.getIdleWaitingPeople().add(this);
    }

    private OrderTicket getOrderTicketByTableId(Table table) {
        //      获取这个桌的订单
        OrderTicket orderTicket = this.restaurantPlus.getOrderTickets().get(table);
        return orderTicket;
    }

    /**
     * 服务员上菜（由大厨调用）
     *
     * @param plate
     * @param table
     */
    public void delivery(Plate plate, Table table) {

        try {
            table.getNewFoodQueue().put(plate);

            Map<Integer, Boolean> foodStatistic = getOrderTicketByTableId(table).getFoodStatistic();
//            标记该桌的这个菜已经上好了
            foodStatistic.replace(plate.getFood().getFoodId(), false, true);

//            送完菜之后自己就没事干了
            this.restaurantPlus.getIdleWaitingPeople().add(this);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
